package com.dashu.school.controller;

import com.dashu.base.bean.excel.ExcelTeacher;
import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.enums.school.excel.ExcelFileType;
import com.dashu.base.enums.school.excel.ExcelType;
import com.dashu.base.utils.excel.ExcelUtil;
import com.dashu.school.dto.PageDTO;
import com.dashu.school.dto.company.CompanyTeacherDTO;
import com.dashu.school.dto.company.CompanyTeacherListDTO;
import com.dashu.school.result.PageEnty;
import com.dashu.school.result.ResultBean;
import com.dashu.school.service.CompanyTeacherService;
import com.dashu.school.vo.UpdateEntityVO;
import com.dashu.school.vo.company.CompanyTeacherListVO;
import com.dashu.school.vo.company.CompanyTeacherVO;
import com.dashu.school.vo.company.UpCompanyTeacherVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/companyTeacher")
@RestController
@Slf4j
@Api(value = "dashu/companyTeacher", tags = {"组织教师模块"}, description = "后台")
public class CompanyTeacherController {

    @Autowired
    private CompanyTeacherService companyTeacherService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addTeacher")
    @ApiOperation(value = "教师添加", notes = "教师添加")
    public ResultBean<CompanyTeacherDTO> addTeacher(@RequestBody @Validated CompanyTeacherVO companyTeacherVO) {
        if (companyTeacherVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师添加", companyTeacherService.add(companyTeacherVO));
    }

    @PostMapping("/addTeachers")
    @ApiOperation(value = "批量添加", notes = "批量添加")
    public ResultBean<List<CompanyTeacherDTO>> addPersonals(@RequestParam(value = "pic") MultipartFile file, long companyId) throws IOException {
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf(".")).replace(".", "");
        if (fileType == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        List<ExcelTeacher> excelTeachers = ExcelUtil.readExcel(file.getInputStream(), ExcelFileType.getExcelFileType(fileType), ExcelType.TEACHER);
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "批量添加", companyTeacherService.batchAdd(excelTeachers, companyId));
    }

    @PostMapping("/updataTeacher")
    @ApiOperation(value = "教师信息修改", notes = "教师信息修改")
    public ResultBean<CompanyTeacherDTO> updataTeacher(@RequestBody @Validated UpdateEntityVO<UpCompanyTeacherVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师信息修改", companyTeacherService.updataByCompanyTeacherId(updateEntityVO));

    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询教师列表", notes = "父节点查询教师列表")
    public ResultBean<List<CompanyTeacherDTO>> findBase(@RequestBody @Validated PageEnty<Long> baseVo) {
        if (baseVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询教师列表", companyTeacherService.findByCreteUser(baseVo));

    }

    @GetMapping("/findTeacher")
    @ApiOperation(value = "教师信息查询", notes = "教师信息查询")
    public ResultBean<CompanyTeacherDTO> findTeacher(long teacherId) {
        if (teacherId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师信息查询", companyTeacherService.findByCompanyTeacherId(teacherId));

    }

    @DeleteMapping("/deleteTeacher")
    @ApiOperation(value = "教师信息删除", notes = "教师信息删除")
    public ResultBean<Boolean> deleteTeacher(long companyTeacherId) {
        if (companyTeacherId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师信息删除", companyTeacherService.deleteByCompanyTeacherId(companyTeacherId));

    }
    @PostMapping("/issueBatchTeacher")
    @ApiOperation(value = "教师信息批量下发", notes = "教师信息批量下发")
    public ResultBean<Boolean> issueBatchTeacher(@RequestBody @Validated List<Long> companyTeacherIds) {
        if (companyTeacherIds == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师信息批量下发", companyTeacherService.issueBatchTeacher(companyTeacherIds));

    }
    @PostMapping("/deleteBatchTeacher")
    @ApiOperation(value = "教师信息批量删除", notes = "教师信息批量删除")
    public ResultBean<Boolean> deleteBatchTeacher(@RequestBody @Validated List<Long> companyTeacherIds) {
        if (companyTeacherIds == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师信息批量删除", companyTeacherService.deleteBatchTeacher(companyTeacherIds));

    }

    @PostMapping("/pageTeacherList")
    @ApiOperation(value = "教师列表", notes = "教师列表")
    public ResultBean<PageDTO<CompanyTeacherListDTO>> pageTeacherList(@RequestBody @Validated PageEnty<CompanyTeacherListVO> companyTeacherListVO) throws Exception {
        PageDTO<CompanyTeacherListDTO> pageDTO = new PageDTO<>();
        if (companyTeacherListVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyTeacherService.countTeacherList(companyTeacherListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyTeacherService.pageTeacherList(companyTeacherListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师列表", pageDTO);

    }
}
